package org.bigwinner.elasticsearch;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.sun.xml.internal.ws.util.xml.CDATA;
import org.bigwinner.elasticsearch.utils.ConfigFileUtil;
import org.bigwinner.elasticsearch.utils.ElasticSearchUtil;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.VersionType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @author: bigwinner
 * @date: 2021/8/11 上午8:41
 * @version: 1.0.0
 * @description: es 版本控制demo
 */
public class VersionControlDemo {
    private static Logger LOGGER = LoggerFactory.getLogger(VersionControlDemo.class);
    public static void main(String[] args) {
        String indexName = ConfigFileUtil.getValue("elasticsearch.indexName");
        RestHighLevelClient restHighLevelClient = ElasticSearchUtil.getRestHighLevelClient();
        BulkProcessor bulkProcessor = ElasticSearchUtil.initBulkProcessor(restHighLevelClient);
        Long create = System.currentTimeMillis();
        System.out.println("当前时间：" + create);
        // 插入一条数据, create为1628691857432
        String data1 = "{\"user\": \"zhangsan\", \"age\": 12, \"create\": \"" + create + "\"}";
        // 更新一条新数据，更新成功，create为1628691952831
        String data2 = "{\"user\": \"lisi\", \"age\": 21, \"create\": \"" + create + "\"}";
        // 更新一条旧数据，create为最开始插入的值1628691857432
        String data3 = "{\"user\": \"wangwu\", \"age\": 33, \"create\": \"1628691857333\"}";
        Map dataMap1 = JSONUtil.parseObj(data1);
        Map dataMap2 = JSONUtil.parseObj(data2);
        Map dataMap3 = JSONUtil.parseObj(data3);


        IndexRequest indexRequest1 = Requests.indexRequest()
                .index(indexName)
                .id("1")
                // 指定更新的
                .version(Long.parseLong(dataMap1.get("create").toString()))
                // 指定使用外部版本号
                .versionType(VersionType.EXTERNAL)
                .source(dataMap1);

        IndexRequest indexRequest2 = Requests.indexRequest()
                .index(indexName)
                .id("1")
                // 指定更新的
                .version(Long.parseLong(dataMap2.get("create").toString()))
                // 指定使用外部版本号
                .versionType(VersionType.EXTERNAL)
                .source(dataMap2);

        IndexRequest indexRequest3 = Requests.indexRequest()
                .index(indexName)
                .id("1")
                // 指定更新的
                .version(Long.parseLong(dataMap3.get("create").toString()))
                // 指定使用外部版本号
                .versionType(VersionType.EXTERNAL)
                .source(dataMap3);

//        bulkProcessor.add(indexRequest1);
//        bulkProcessor.add(indexRequest2);
        bulkProcessor.add(indexRequest3);

    }
}
